<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2013
 * ---
 * Repraesentiert einen Menueintrag
 *
 * @author Robin
 */

class MenuItem {
    
    private $id = null;
    private $name = null;
    private $parent = null;
    private $order = null;
    private $category = null;
    private $menu = null;
    
    public function __construct($data = array()) {
        if (isset($data['menuitem_id'])) $this->id = (int) $data['menuitem_id'];
        if (isset($data['menuitem_name'])) $this->name = $data['menuitem_name'];
        if (isset($data['menuitem_parent'])) $this->parent = (int) $data['menuitem_parent'];
        if (isset($data['menuitem_order'])) $this->order = (int) $data['menuitem_order'];
        if (isset($data['menuitem_category'])) $this->category = (int) $data['menuitem_category'];
        if (isset($data['menuitem_menu'])) $this->menu = (int) $data['menuitem_menu'];
    }
    
    /**
     * 
     * @param type $id
     * @return \MenuItem
     */
    public static function getMenuItemById($id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_menuitems WHERE menuitem_id = :id";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new MenuItem($row);
    }
    
    /**
     * 
     * @param type $numRows
     * @return type
     */
    public static function getMenuItemList($numRows=1000000, $order="menuitem_parent, menuitem_order DESC") {
        $db = new Database();

        $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM ff_menuitems ORDER BY " .mysql_real_escape_string($order)." LIMIT :numRows";

        $stmt = $db->prepare( $sql );
        $stmt->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $menuItem = new MenuItem($row);
            $list[] = $menuItem;
        }
        
        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;

        return (array("menuItemList" => $list, "totalRows" => $totalRows[0]));
    }
    
    /**
     * 
     */
    public function insert() {
        if (!is_null($this->id)) {
            trigger_error("MenuItm::insert(): Es wird versucht ein MenuItem zu speichern, das eine ID hat, die bereits existiert (ID: $this->id).", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "INSERT INTO ff_menuitems (menuitem_name, menuitem_parent, menuitem_order, menuitem_category, menuitem_menu, menuitem_site) VALUES (:name, :parent, :order, :category, :menu, :site)";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":name", $this->name, PDO::PARAM_STR);
        $stmt->bindValue(":parent", $this->parent, PDO::PARAM_INT);
        $stmt->bindValue(":order", $this->order, PDO::PARAM_INT);
        $stmt->bindValue(":category", $this->category, PDO::PARAM_INT);
        $stmt->bindValue(":menu", $this->menu, PDO::PARAM_INT);
        $stmt->bindValue(":site", $this->site, PDO::PARAM_INT);
        $stmt->execute();
        
        $this->id = $db->lastInsertId();
        
        $db = null;
    }
    
    /**
     * 
     */
    public function update() {
        if (is_null($this->id)) {
            trigger_error("MenuItem::update(): Es wird versucht ein MenuItem zu speichern, das keine ID hat - was nicht geht beim Update.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "UPDATE ff_menuitems SET menu_name=:name, menuitem_parent=:parent, menuitem_order:=order, menuitem_category:=category, menuitem_menu=:menu, menuitem_site=:site WHERE menuitem_id=:id";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":name", $this->name, PDO::PARAM_STR);
        $stmt->bindValue(":parent", $this->parent, PDO::PARAM_INT);
        $stmt->bindValue(":order", $this->order, PDO::PARAM_INT);
        $stmt->bindValue(":category", $this->category, PDO::PARAM_INT);
        $stmt->bindValue(":menu", $this->menu, PDO::PARAM_INT);
        $stmt->bindValue(":site", $this->site, PDO::PARAM_INT);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
    
    /**
     * 
     */
    public function delete() {
        if (is_null($this->id)) {
            trigger_error("MenuItem::delete(): Es wird versucht ein MenuItem zu loeschen, das keine ID hat - was nicht geht beim Loeschen.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "DELETE FROM ff_menuitems WHERE menuitem_id = :id LIMIT 1";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getParent() {
        return $this->parent;
    }

    public function getOrder() {
        return $this->order;
    }

    public function getCategory() {
        return $this->category;
    }

    public function getMenu() {
        return $this->menu;
    }
    
}

?>
